﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EOSCRM.Util
{
    public class ReportingHelper
    {
        /// <summary>
        /// Dictionary to hold the Group Name and the page number at which this group ends
        /// </summary>
        /// <remarks></remarks>
        private static Dictionary<string, string> _GroupWithRunningMaxPageNumber = new Dictionary<string, string>();
        /// <summary>
        /// Dictionary to hold the Group Name and the Total number of pages in that group
        /// </summary>
        /// <remarks></remarks>

        private static Dictionary<string, string> _GroupWithTotalPageNumber = new Dictionary<string, string>();
        /// <summary>
        /// Adds the group and the page number into the shared/static dictionary collection
        /// The groupname / id being the key and pagenumber as value
        /// If the groupname already exists then the GroupCurrentPageNumber value will be reset with the
        /// surrent value. By doing so, always we will have the maximum value in each group
        /// which will be the group's total number of page once the report is processed completely
        /// This method is being called from the code in the rdlc file
        /// </summary>
        /// <param name="group"></param>
        /// <param name="groupCurrentPageNumber"></param>
        /// <remarks></remarks>
        public void Add(string @group, string groupCurrentPageNumber)
        {
            if (_GroupWithRunningMaxPageNumber.ContainsKey(@group))
            {
                _GroupWithRunningMaxPageNumber[@group] = groupCurrentPageNumber;
            }
            else
            {
                _GroupWithRunningMaxPageNumber.Add(@group, groupCurrentPageNumber);
            }
        }
        /// <summary>
        /// This Gives the Group and the Total number of pages within the group
        /// Internally this function reads the data stored in the dictionary <see>_GroupAndMaxPageNumberWithInTheGroup</see>
        /// and computes the result.
        /// <see>_GroupAndMaxPageNumberWithInTheGroup</see> will have the data like below which is the input for this function
        /// [group-A,04] ------- (1)
        /// [group-B,09] ------- (2)
        /// [group-C,10] ------- (3)
        /// This function iterates the <see>_GroupAndMaxPageNumberWithInTheGroup</see> collection and computes the
        /// total number of pages by substracting the page number with the previous page number like below
        /// At Iteration-01
        ///          Copys as it is from _GroupWithRunningMaxPageNumber into _GroupWithTotalPageNumber
        ///          The result will be like [group-A,04]
        /// At Iteration-02
        ///          It substracts (2) - (1) i.e. 09-04=05
        ///          The result will be like
        ///                                [group-A,04]
        ///                                [group-B,05]
        /// At Iteration-03
        ///          It substracts (3) - (2) i.e. 10-09=01
        ///          The result will be like
        ///                                [group-A,04]
        ///                                [group-B,05]
        ///                                [group-C,01]
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public Dictionary<string, string> GetGroupWithTotalPageNumber()
        {
            if (_GroupWithTotalPageNumber.Count == 0)
            {
                string preKey = string.Empty;
                foreach (var dic in _GroupWithRunningMaxPageNumber)
                {
                    //dic = dic_loopVariable;
                    if (string.IsNullOrEmpty(preKey))
                    {
                        _GroupWithTotalPageNumber.Add(dic.Key, dic.Value);
                        preKey = dic.Key;
                    }
                    else
                    {
                        _GroupWithTotalPageNumber.Add(dic.Key, (Convert.ToInt32(dic.Value) - Convert.ToInt32(_GroupWithRunningMaxPageNumber[preKey])).ToString());
                        preKey = dic.Key;
                    }
                }
            }
            return _GroupWithTotalPageNumber;
        }
        /// <summary>
        /// This gives the Total number of pages for a given group(key)
        /// This looks up at the dictionary <see>_GroupWithTotalPageNumber</see> and returns the value for the matching key
        /// This is called from the code residing in the rdlc for each page to print the total number of pages
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public string GetTotalPageNumber(string key)
        {
            if (!string.IsNullOrEmpty(key) && _GroupWithTotalPageNumber.ContainsKey(key))
            {
                return _GroupWithTotalPageNumber[key];
            }
            else
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// Resets the shared variables.
        /// </summary>
        /// <remarks></remarks>
        public void Clear()
        {
            _GroupWithRunningMaxPageNumber.Clear();
            _GroupWithTotalPageNumber.Clear();
        }
    }
}
